package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.service.CategoryService;
import java.util.List;
import javax.security.auth.callback.LanguageCallback;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分类管理
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        //日志
        log.info("category: {}",category);

        //调用方法
        categoryService.save(category);

        return R.success("新增分类成功！");
    }


    /**
     * 分页查询所有
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        log.info("当前页码：{}，每页展示数：{}",page,pageSize);

        //构造分页构造器
        Page<Category> pageInfo = new Page<Category>();

        //构造条件构造器  升序
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>();
        queryWrapper.orderByAsc(Category::getSort);  //根据升序排序

        //调用方法
        categoryService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);

    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(Long id){
        //
        log.info("id值： {}",id);

        //调用方法
        //categoryService.removeById(id);

        //Service层自定义的方法
        categoryService.remove(id);

        return R.success("分类信息删除成功");
    }


    /**
     * 修改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        //
        log.info("修改信息： {}",category);

        //调用方法
        categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }


    /**
     * 根据菜品分类type=1查询所有菜品分类   回显数据到添加菜品页面右上角
     * 将传过来的 url?type = 1 封装到categry实体或者 String type 都可以接收
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){

        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>();

        //添加具体条件
        queryWrapper.eq(category.getType() !=null,Category::getType,category.getType());
        //添加  排序条件         升序                           修改时间降序
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //调用方法
        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }

}
